package com.ccp.dev.workflow.service;

import com.ccp.dev.core.util.BeanUtils;
import com.ccp.dev.system.dao.PositionDao;
import com.ccp.dev.system.dao.SysOrgDao;
import com.ccp.dev.system.dao.SysRoleDao;
import com.ccp.dev.system.dao.SysUserDao;
import com.ccp.dev.system.model.Position;
import com.ccp.dev.system.model.SysOrg;
import com.ccp.dev.system.model.SysRole;
import com.ccp.dev.system.model.SysUser;
import com.ccp.dev.workflow.bpmconst.BpmConst;
import com.ccp.dev.workflow.dao.TaskUserDao;
import com.ccp.dev.workflow.model.TaskUser;
import com.ccp.dev.workflow.model.bpm.TaskExecutor;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * ClassDescribe:任务所属用户 Service类
 * @author :wangtao
 * Date: 2019-7-24
 * Since:1
 */
@Service
public class TaskUserService {

    @Resource
    private TaskUserDao taskUserDao;
    @Resource
    private SysUserDao sysUserDao;
    @Resource
    private SysOrgDao sysOrgDao;
    @Resource
    private PositionDao positionDao;
    @Resource
    private SysRoleDao sysRoleDao;


    public List<TaskUser> getByTaskId(String taskId){

        return taskUserDao.getByTaskId(taskId);
    }
    /**
     * 根据任务Id获取任务候选人。
     * @param taskId 任务id
     * @return 用户集合
     */
    public Set<SysUser> getCandidateUsers(String taskId){
        Set<SysUser> taskUserSet=new HashSet<>();
        List<TaskUser> taskUsers=getByTaskId(taskId);
        for(TaskUser taskUser:taskUsers){
            if(taskUser.getUserId()!=null
                    && !BpmConst.EMPTY_USER.equals(taskUser.getUserId())){
                SysUser sysUser=sysUserDao.getById(taskUser.getUserId());
                taskUserSet.add(sysUser);
            }else if(taskUser.getGroupId()!=null){
                List<SysUser> userList = null;
                if(TaskExecutor.USER_TYPE_ORG.equals(taskUser.getType())){
                    //组织下的用户
                     userList= sysUserDao.queryByOrgId(taskUser.getGroupId());
                }else if(TaskExecutor.USER_TYPE_POS.equals(taskUser.getType())){
                    //岗位下的用户
                     userList= sysUserDao.queryByPosId(taskUser.getGroupId());
                }else if(TaskExecutor.USER_TYPE_ROLE.equals(taskUser.getType())){
                    //角色下的用户
                     userList=sysUserDao.queryByRoleId(taskUser.getGroupId());
                }
                if(null!=userList){
                    taskUserSet.addAll(userList);
                }
            }
        }
        return taskUserSet;
    }

    /**
     * 取得任务的候选用户
     * @param taskId 任务id
     * @return 任务执行人集合
     */
    public Set<TaskExecutor> getCandidateExecutors(String taskId){

        Set<TaskExecutor> taskUserSet=new HashSet<>();
        List<TaskUser> taskUsers=getByTaskId(taskId);
        for(TaskUser taskUser:taskUsers){
            if(taskUser.getUserId()!=null && !BpmConst.EMPTY_USER.equals(taskUser.getUserId())){
                SysUser sysUser=sysUserDao.getById(taskUser.getUserId());
                if(BeanUtils.isNotEmpty(sysUser)){
                    taskUserSet.add(TaskExecutor.getTaskUser(taskUser.getUserId(), sysUser.getFullName()));
                }
            }else if(taskUser.getGroupId()!=null){
                String tmpId=taskUser.getGroupId();
                if(TaskExecutor.USER_TYPE_ORG.equals(taskUser.getType())){
                    //组织下的用户
                    SysOrg sysOrg=sysOrgDao.getById(tmpId);
                    taskUserSet.add(TaskExecutor.getTaskOrg(tmpId, sysOrg.getOrgName()));
                }else if(TaskExecutor.USER_TYPE_POS.equals(taskUser.getType())){
                    //岗位下的用户
                    Position position=positionDao.getById(tmpId);
                    taskUserSet.add(TaskExecutor.getTaskPos(tmpId, position.getPosName()));
                }else if(TaskExecutor.USER_TYPE_ROLE.equals(taskUser.getType())){
                    //角色下的用户
                    SysRole sysRole=sysRoleDao.getById(tmpId);
                    taskUserSet.add(TaskExecutor.getTaskRole(tmpId, sysRole.getRoleName()));
                }
            }
        }
        return taskUserSet;
    }
}
